<template>
  <div class="flexContent">
    <div class="searchBox">
      <el-form ref="formRef" class="demo-form-inline" @submit.native.prevent label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="手机号">
                  <el-input v-model="phone" @keyup.enter.native="searchFn" clearable style="width: 95%"
                    placeholder="请输入..." />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="状态">
                  <el-select v-model="status" clearable style="width: 95%" popper-class="eloption" placeholder="请选择..."
                    filterable>
                    <el-option value="0" label="待使用" />
                    <el-option value="1" label="已使用" />
                    <el-option value="2" label="已过期" />
                  </el-select>
                </el-form-item>
              </el-col>
              <!-- <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="兑换门店">
                  <el-select v-model="shopId" clearable style="width: 95%" popper-class="eloption" placeholder="请选择..."
                    filterable>
                    <el-option v-for="shop in shopList" :key="shop.id" :value="shop.id" :label="shop.name" />
                  </el-select>
                </el-form-item>
              </el-col> -->
            </el-row>
          </div>
          <div class="btnBox">
            <el-button class="searchBtn" icon="el-icon-search" @click="searchFn">搜索</el-button>
            <el-button class="resetBtn" icon="el-icon-refresh-right" @click="resetFn">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <!-- <div class="add">
      <el-button class="addBtn" @click="addFn">新增</el-button>
    </div> -->
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" class="tablebox" border ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          align="center">
          <vxe-column type="seq" title="序号" width="80"></vxe-column>
          <vxe-column field="userName" title="会员名称">
            <template v-slot="{ row }">{{
              row.memberName ? row.memberName : row.memberPhone
            }}</template>
          </vxe-column>
          <vxe-column field="memberPhone" title="手机号" />
          <vxe-column field="consumeNum" title="兑换数量" />
          <vxe-column field="commodityName" title="兑换商品" />
          <vxe-column field="status" title="状态">
            <template v-slot="{ row }">
              <el-tag v-if="row.status == '0'" type="info">待使用</el-tag>
              <el-tag v-if="row.status == '1'" type="success">已使用</el-tag>
              <el-tag v-if="row.status == '2'" type="error">已过期</el-tag>
            </template>
          </vxe-column>
          <vxe-column field="status" title="兑换门店">
            <template v-slot="{ row }">
              <span v-if="row.status == '1'">{{ row.storeName }}</span>
              <span v-if="row.status != '1'">-</span>
            </template>
          </vxe-column>
          <vxe-column field="consumerTime" title="核销时间">
            <template v-slot="{ row }">
              <span v-if="row.status == '1'">{{ row.consumerTime | parseTime }}</span>
              <span v-if="row.status != '1'">-</span>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import {
  getExchangeRecord,
  getShopEasy
} from "@/api/shop";
export default {
  name: "MemberInfo",
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      phone: "",
      memberList: [],
      isSearch: false,
      shopId: "",
      status: '',
      shopList: [],
    };
  },
  created() {
    this.getExchangeRecord();
    this.getShopEasy();
  },
  methods: {
    async getShopEasy() {
      const res = await getShopEasy();
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        console.log(result);
        this.shopList = result.store;
        console.log("shopList", this.shopList);
      } else {
        // this.$message.error(res.message);
      }
    },
    // 获取会员列表
    async getExchangeRecord() {
      if (!this.isSearch) {
        this.phone = "";
        this.shopId = "";
        this.status = "";
      }
      const res = await getExchangeRecord({
        ...this.page,
        phone: this.phone,
        // storeId: this.shopId,
        status: this.status,
      });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.memberList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("memberList", this.memberList);
        const $table = this.$refs.xTable1
        $table.loadData(this.memberList);
      } else {
        // this.$message.error(res.message);
      }
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getExchangeRecord();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getExchangeRecord();
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };

      this.isSearch = true;
      this.getExchangeRecord()
    },
    resetFn() {
      this.phone = "";
      this.shopId = "";
      this.status = "";
      this.isSearch = false;
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.memberList = [];
      this.getExchangeRecord();
    },
  },
};
</script>

<style lang="scss" scoped></style>
